package com.ce.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class FileResDownload {
	public static List<String> errorList = new ArrayList<String>();
	public static String SDPATH = "E:\\wypsmall\\Code\\Code_j2ee_e34\\NewEtvSDExport\\";
	public static String filename = "etvdata.properties";
	//public static String expfilename = "etvdata-exp.properties";
	public static String expfile = "etvdata-exp";
	public static String expext = ".properties";
	public static String dir = "etvsd/";
	public static String imagedir = "etvsd\\image\\";
	public static String filedir = "etvsd\\file\\";
	private static int FILESIZE = 1 * 1024;
	public static void downloadPic(String url) {
		try {
			if(url == null || url.length() == 0)
				return;
			if(url.indexOf("http") == -1)
				url = "http://"+url;
			URL myFileUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
			conn.setConnectTimeout(0);   
			conn.setDoInput(true);   
			conn.connect();
			InputStream is = conn.getInputStream();
			String filename = url.substring(url.lastIndexOf("/")+1);
			writeImageInSDCard(filename,is);
			is.close(); 
		} catch (Exception e) {
			e.printStackTrace();
			errorList.add(url);
		} 
	} 
	public static void downloadSwf(String url) {
		try {
			if(url == null || url.length() == 0)
				return;
			if(url.indexOf("http") == -1)
				url = "http://"+url;
			int posb = url.indexOf("data");
			int pose = url.indexOf("&vid");
			String source = "http://etv.easy3c.net/v/"+url.substring(posb+5, pose);
			System.out.println(source);
			URL myFileUrl = new URL(source);
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
			conn.setConnectTimeout(0);   
			conn.setDoInput(true);   
			conn.connect();
			InputStream is = conn.getInputStream();
			String filename = source.substring(source.lastIndexOf("/")+1);
			System.out.println(filename);
			writeFileInSDCard(filename,is);
			is.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	public static void writeFileInSDCard(String filename, InputStream input) {
		try {
			write2SDFromInput(filedir, filename, input);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void writeImageInSDCard(String filename, InputStream input) {
		try {
			write2SDFromInput(imagedir, filename, input);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static File write2SDFromInput(String path,String fileName,InputStream input){   
    	if(input == null)
    		return null;
        File file = null;   
        OutputStream output = null;   
        try {   
            createSDDir(path);   
            file = createSDFile(path + fileName);   
            output = new FileOutputStream(file);   
            byte[] buffer = new byte[FILESIZE];  
            int count = 0;
            while((count = input.read(buffer))!= -1){  
            	//Thread.sleep(100);
                output.write(buffer,0,count);
                //output.flush();
            }   
            output.flush();   
        }    
        catch (Exception e) {   
            e.printStackTrace();   
        }   
        finally{   
            try {   
                output.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
        return file;   
    }   
	
	 
    public static boolean isDirExist(String fileName){   
        File file = new File(SDPATH + fileName);   
        return file.isDirectory();   
    } 

    public static boolean isFileExist(String fileName){   
        File file = new File(SDPATH + fileName);   
        return file.exists();   
    } 
  
    public static File createSDFile(String fileName) throws IOException{   
        File file = new File(SDPATH + fileName);   
        file.createNewFile(); 
        return file;   
    }  
 
    public static File createSDDir(String dirName){   
        File dir = new File(SDPATH + dirName);   
        dir.mkdir();   
        return dir;   
    } 
}
